home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / x2ftp / msdos / libs / pdcurs21 / portable / wgetstr.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-10-30  |  4.6 KB  |  158 lines

  1. #define CURSES_LIBRARY  1
  2. #include <curses.h>
  3. #undef  wgetstr
  4.  
  5. #ifdef PDCDEBUG
  6. char *rcsid_wgetstr = "$Header: C:\CURSES\portable\RCS\wgetstr.c 2.1 1993/06/18 20:19:27 MH Rel MH $";
  7. #endif
  8.  
  9.  
  10.  
  11.  
  12. /*man-start*********************************************************************
  13.  
  14.   wgetstr()     - read string
  15.  
  16.   X/Open Description:
  17.         A series of characters are read until a newline or carriage
  18.         return is received.  The resulting value is placed in the area
  19.         pointed to by the character pointer str.  The user's erase (^H)
  20.         and kill (^U) characters are interpreted.
  21.  
  22.         NOTE: getstr(), mvgetstr(), and mvwgetstr() are macros.
  23.  
  24.         WARNING:  There is no way to know how long the buffer passed to 
  25.                   this function is, so it is possible to overwrite wrong 
  26.                   memory or code!!
  27.  
  28.   PDCurses Description:
  29.         Tabs are automatically expanded to spaces.
  30.  
  31.   X/Open Return Value:
  32.         These functions return OK on success and ERR on error.
  33.  
  34.   PDCurses Errors:
  35.         It is an error to call this function with a NULL window pointer.
  36.  
  37.   Portability:
  38.         PDCurses        int wgetstr( WINDOW* win, char* str );
  39.         X/Open Dec '88  int wgetstr( WINDOW* win, char* str );
  40.         BSD Curses      int wgetstr( WINDOW* win, char* str );
  41.         SYS V Curses    int wgetstr( WINDOW* win, char* str );
  42.  
  43. **man-end**********************************************************************/
  44.  
  45.  
  46.  
  47. int     wgetstr(WINDOW *win, char *str)
  48. {
  49.         int     ch, i, n;
  50.         int     t = win->_tabsize;
  51.         int     x = win->_curx;
  52.         char*   p = str; 
  53.         bool    stop = FALSE;
  54.         bool    oldecho;
  55.         bool    oldcbreak;
  56.         bool    oldnodelay;
  57.  
  58. #ifdef PDCDEBUG
  59.         if (trace_on) PDC_debug("wgetstr() - called\n");
  60. #endif
  61.  
  62.         if (win == (WINDOW *)NULL)
  63.                 return (ERR);
  64.  
  65. #ifdef UNIX
  66.         wrefresh(win);
  67.  
  68.         while ((*str = wgetch(win)) != ERR && *str != '\n')
  69.              ;
  70.         if (*str == ERR) {
  71.                 *str = '\0';
  72.                 waddstr(win,p);
  73.                 return ERR;
  74.         }
  75.         *str = '\0';
  76.         waddstr(win,p);
  77.         return OK;
  78.  
  79. #else
  80.         oldcbreak       = _cursvar.cbreak;      /* remember states       */
  81.         oldecho         = _cursvar.echo;
  82.         oldnodelay      = win->_nodelay;
  83.  
  84.         _cursvar.echo   = FALSE;                /* we do echo ourselves */
  85.         cbreak();               /* ensure each key is returned immediately */
  86.         win->_nodelay   = FALSE;                /* don't return -1       */
  87.  
  88.         wrefresh (win);
  89.  
  90.         while (!stop)
  91.         {
  92.           switch (ch = wgetch (win) & A_CHARTEXT)
  93.           {
  94.             case '\t':
  95.               ch = ' ';
  96.               n = t - (win->_curx - x)%t;
  97.               for (i=0; i<n; i++) 
  98.               {
  99.                 if (oldecho) waddch (win, ch); 
  100.                 *p++ = ch; 
  101.               }
  102.               break;
  103.  
  104.             case _ECHAR:  /* CTRL-H */   /* Delete character */
  105.               if (p > str)
  106.               {
  107.                 if (oldecho) waddstr (win, "\b \b");
  108.                 ch = *--p;
  109.                 if ((ch < ' ') && (oldecho)) waddstr (win, "\b \b");
  110.               }
  111.               break;
  112.  
  113.             case _DLCHAR:  /* CTRL-U */  /* Delete line */
  114.               while (p > str)
  115.               {
  116.                 if (oldecho) waddstr (win, "\b \b");
  117.                 ch = *--p;
  118.                 if ((ch < ' ') && (oldecho)) waddstr (win, "\b \b");
  119.               }
  120.               break;
  121.  
  122.             case _DWCHAR: /* CTRL-W */  /* Delete word */
  123.               while ((p > str) && (*(p-1) == ' '))
  124.               {
  125.                 if (oldecho) waddstr (win, "\b \b");
  126.                 --p; /* remove space */
  127.               }
  128.               while ((p > str) && (*(p-1) != ' '))
  129.               {
  130.                 if (oldecho) waddstr (win, "\b \b");
  131.                 ch = *--p;
  132.                 if ((ch < ' ') && (oldecho)) waddstr (win, "\b \b");
  133.               }
  134.               break;
  135.  
  136.             case '\n':
  137.             case '\r':
  138.               stop = TRUE;
  139.               if (oldecho) waddch (win, '\n');
  140.               break;
  141.  
  142.             default:
  143.               *p++ = ch;
  144.               if (oldecho) waddch (win, ch);
  145.               break;
  146.           }
  147.           wrefresh (win);
  148.         }
  149.         *p = '\0';
  150.  
  151.         _cursvar.echo   = oldecho;      /* restore old settings */
  152.         _cursvar.cbreak = oldcbreak;
  153.         win->_nodelay   = oldnodelay;
  154.  
  155.         return (OK);
  156. #endif
  157. }
  158.